class DisciplinesController < ApplicationController
  before_filter :login_required, :except => [:index, :college, :show]
  access_control :destroy => 'admin'
  
  # GET /disciplines
  # GET /disciplines.xml
  def index
    @colleges = College.find(:all, :order => 'name ASC')
  end

  def college
    @college = College.find(params[:id])
    @disciplines = Discipline.find_all_by_college_id(params[:id])
  end

  # GET /disciplines/1
  # GET /disciplines/1.xml
  def show
    @discipline = Discipline.find(params[:id])
    @college = @discipline.college
    @members = @discipline.users.recent
    @articles = @discipline.articles.paginate :page => params[:article_page], :order => 'created_at DESC'
    @discussions = @discipline.discussions.paginate :page => params[:discussion_page], :order => 'created_at DESC'
  end

  # GET /disciplines/new
  def new
    @discipline = Discipline.new(:college_id => params[:college_id])
  end

  # GET /disciplines/1;edit
  def edit
    @discipline = Discipline.find(params[:id])
  end

  # POST /disciplines
  # POST /disciplines.xml
  def create
    @discipline = Discipline.new(params[:discipline])
    @discipline.user = current_user

    respond_to do |format|
      if @discipline.save
        @membership = Membership.create(:user => current_user, :discipline => @discipline)
        flash[:notice] = 'Discipline was successfully created.'
        format.html { redirect_to discipline_url(@discipline) }
      else
        format.html { render :action => "new" }
      end
    end
  end

  # PUT /disciplines/1
  # PUT /disciplines/1.xml
  def update
    @discipline = Discipline.find(params[:id])

    respond_to do |format|
      if @discipline.update_attributes(params[:discipline])
        flash[:notice] = 'Discipline was successfully updated.'
        format.html { redirect_to discipline_url(@discipline) }
      else
        format.html { render :action => "edit" }
      end
    end
  end

  def destroy
    @discipline = Discipline.find(params[:id])
    @discipline.destroy
    flash[:notice] = "Discipline was sucessfully deleted."

    respond_to do |format|
      format.html { redirect_to disciplines_path }
    end
  end

  def leave 
    @discipline = Discipline.find(params[:id])
    @membership = current_user.memberships.find_by_discipline_id(@discipline.id)
    @membership.destroy

    respond_to do |format|
      flash[:notice] = "You have left #{@discipline.name}"
      format.html { redirect_to discipline_url(@discipline) }
    end
  end

  def invite
    @discipline = Discipline.find(params[:id])
    @invite = DisciplineInvite.new
  end

  def send_invite
    @discipline = Discipline.find(params[:id])
    @invite = DisciplineInvite.new(params[:invite])
    if @invite
      @invite.emails.each do |email|
        UserMailer.deliver_invite_to_discipline(:message => @invite.message, :email_address => email.strip, :from => current_user, :discipline => @discipline, :link => new_discipline_membership_url(@discipline))
      end
      flash[:notice] = "We have sent off your invitations to #{@discipline.name}"
      redirect_to discipline_path(@discipline)
    else
      render :action => 'invite'
    end
  end
  
  protected

  def authorized?
    logged_in? and current_user.admin? or (params[:id].nil? || Discipline.find(params[:id]).user == current_user) or %w(invite send_invite leave index new create show college).include?(action_name)
  end

end
